package com.graduation.vaccine.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.graduation.vaccine.dto.SysCompanyDTO;
import com.graduation.vaccine.dto.VaccinedAddressDTO;
import com.graduation.vaccine.exception.GlobalException;
import com.graduation.vaccine.mapper.CompanyMapper;
import com.graduation.vaccine.pojo.Company;
import com.graduation.vaccine.service.CompanyService;
import com.graduation.vaccine.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZhaoJunYao
 * @since 2022-04-13
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService {


    @Autowired
    private CompanyMapper companyMapper;

    /**
     * 获取该用户所在城市的所有有余量的该疫苗的接种点信息
     * @param vaccinedAddressVo
     * @return
     */
    @Override
    public List<VaccinedAddressDTO> getTotalVaccinedAddress(VaccinedAddressVo vaccinedAddressVo) {
        //获取列表
        return companyMapper.getTotalVaccinedAddress(vaccinedAddressVo);
    }

    /**
     * 获取用户附近的接种点
     * @param nearByAddressVo
     * @return
     */
    @Override
    public RespBean getNearByVaccinedAddress(NearByAddressVo nearByAddressVo) {
        return RespBean.success(companyMapper.getNearByVaccinedAddress(nearByAddressVo));
    }

    @Override
    public RespBean getAll() {
        return RespBean.success(companyMapper.getAll());
    }

    @Override
    public RespBean companyDropDownBox() {
        return RespBean.success(companyMapper.companyDropDownBox());
    }

    @Override
    public RespBean unionQuery(CompanyUnionQueryVo companyUnionQueryVo) {
        return RespBean.success(companyMapper.unionQuery(companyUnionQueryVo));
    }

    @Override
    public RespBean updateCompany(UpdateCompanyVo updateCompanyVo) {
        int row = companyMapper.updateCompany(updateCompanyVo);
        if(row<1){
            return RespBean.error(RespBeanEnum.UPDATE_ERROR,"部门信息提交失败！请检查数据输入是否规范");
        }
        return RespBean.success();
    }

    @Override
    public RespBean registerDepartment(RegisterCompanyVo registerCompanyVo) {
        //判断部门是否注册过
        SysCompanyDTO flag=companyMapper.getCompanyByName(registerCompanyVo.getName());
        if(null!=flag){
            return RespBean.error(RespBeanEnum.COMPANY_REPEAT);
        }


        Company company=new Company();
        company.setName(registerCompanyVo.getName());
        company.setAddress(registerCompanyVo.getAddress());
        company.setWorktimeDesc(registerCompanyVo.getWorktimeDesc());
        company.setTelephone(registerCompanyVo.getTelephone());
        company.setImgUrl(registerCompanyVo.getImgUrl());
        company.setLongitude(registerCompanyVo.getLongitude());
        company.setLatitude(registerCompanyVo.getLatitude());
        company.setDepartmentcode(registerCompanyVo.getDepartmentcode());

        try {
            if (companyMapper.addOne(company)<1) {
                return RespBean.error(RespBeanEnum.UPDATE_ERROR,"创建失败");
            }
        } catch (Exception e) {
            throw new GlobalException(RespBeanEnum.BIN_ERROR);
        }
        return RespBean.success("注册成功！");

    }

    @Override
    public RespBean getCompanyByName(String companyName) {
        return RespBean.success(companyMapper.getCompanyByName(companyName));
    }
}
